package gowcage.mvp.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.gowcage.testandroid.R;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gowcage.mvp.presenter.IPresenter;
import gowcage.mvp.presenter.Presenter;

/**
 * Created by Administrator on 2017/4/8.
 * View 只负责界面的显示
 */

public class ViewActivity extends Activity implements IView {

    //BindViews-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    @BindViews({R.id.test_edit_username, R.id.test_edit_password})
    List<EditText> editList;
    @BindViews({R.id.test_btn_login, R.id.test_btn_clear})
    List<Button> btnList;
    @BindView(R.id.test_progressbar)
    ProgressBar progressBar;
    //BindViews-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    final String TAG = "ViewActivity";
    IPresenter iPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        iPresenter = new Presenter(this);
        iPresenter.setProgressBarVisiblity(View.INVISIBLE);
    }

    @Override
    public void onLoginResult(Boolean result, int code) {
        iPresenter.setProgressBarVisiblity(View.INVISIBLE);
        for (Button btn : btnList) {
            btn.setEnabled(true);
        }
        if (result) {
            Toast.makeText(this, "Login Success", Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(this, "Login Fail, errorCode = " + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClearText() {
        for (EditText edit : editList) {
            edit.setText("");
        }
    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
        progressBar.setVisibility(visibility);
    }

    /*
   监听
     */

    @OnClick({R.id.test_btn_login, R.id.test_btn_clear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.test_btn_login:
                iPresenter.setProgressBarVisiblity(View.VISIBLE);
                for (Button btn : btnList) {
                    btn.setEnabled(false);
                }
                iPresenter.doLogin(editList.get(0).getText().toString(), editList.get(1).getText().toString());
                Log.i(TAG, "name = " + editList.get(0).getText().toString() + "/pwd = " + editList.get(1).getText().toString());
                break;
            case R.id.test_btn_clear:
                iPresenter.clear();
                break;
        }
    }
}
